<?php
  class PersonService{
		function getPersonByMooAndHouseNo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo, $selectHouseNo){
      $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/group-2.png";
			
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();
				
				$sql = "
								 SELECT
									pn_degis_contact.aco_id,								 
									pn_degis_contact.aco_person_id,
									pn_degis_contact.aco_addr_house_no,
									pn_degis_contact.aco_first_name,
									pn_degis_contact.aco_last_name,
									pn_degis_gender.agr_name,
									pn_degis_contact.aco_birthdate,
									pn_degis_contact.aco_telephone,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon
								 FROM 
									samuidemap.pn_degis_contact,
									samuidemap.pn_degis_gender,
                  samuidemap.pn_degis_tumbol
								WHERE  
									pn_degis_contact.aco_tumbol_id = $selectTambon
								AND
									pn_degis_contact.aco_addr_moo = $selectMoo							
								AND
									pn_degis_contact.aco_addr_house_no = '$selectHouseNo'
								AND
									pn_degis_contact.aco_gender_id = pn_degis_gender.agr_id
                AND
                  pn_degis_tumbol.atb_id = $selectTambon 
								ORDER BY 
									pn_degis_contact.aco_first_name";
									
					//echo $sql; exit;
					$column = array('id', 'pid', 'house_no', 'first_name', 'last_name', 'gender', 'birthdate', 'telephone', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						return $objectArray;
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';
				}  		
		}

		function getPersonByMooAndUnitAndHouseNo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo, $selectUnit, $selectHouseNo){
      $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/group-2.png";
			
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();
				
				$sql = "
								 SELECT
									pn_degis_contact.aco_id,									 
									pn_degis_contact.aco_person_id,
									pn_degis_contact.aco_addr_house_no,																		
									pn_degis_contact.aco_first_name,
									pn_degis_contact.aco_last_name,
									pn_degis_gender.agr_name,
									pn_degis_contact.aco_birthdate,
									pn_degis_contact.aco_telephone,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon
								 FROM 
									samuidemap.pn_degis_contact,
									samuidemap.pn_degis_gender,
                  samuidemap.pn_degis_tumbol
								WHERE  
									pn_degis_contact.aco_tumbol_id = $selectTambon
								AND
									pn_degis_contact.aco_addr_moo = $selectMoo
								AND
								  pn_degis_contact.aco_unit = $selectUnit										
								AND
									pn_degis_contact.aco_addr_house_no = '$selectHouseNo'
                AND
                  pn_degis_tumbol.atb_id = pn_degis_contact.aco_tumbol_id
								AND
									pn_degis_contact.aco_gender_id = pn_degis_gender.agr_id
                AND
                  pn_degis_tumbol.atb_id = $selectTambon 
								ORDER BY 
									pn_degis_contact.aco_first_name";
									
					//echo $sql; exit;
					$column = array('id', 'pid', 'house_no', 'first_name', 'last_name', 'gender', 'birthdate', 'telephone', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						return $objectArray;
						//$this->setResponse($objectArray, $select, $select_moo);
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}  				
		}
				

		function getPersonByMooAndUnitAndHouseNoForWriteXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo, $selectUnit, $selectHouseNo, $xmlDirectoryPath, $xmlFileName){
      $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/group-2.png";
			
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();
				
				$sql = "
								 SELECT
									pn_degis_contact.aco_id,									 
									pn_degis_contact.aco_person_id,
									pn_degis_contact.aco_addr_house_no,																		
									pn_degis_contact.aco_first_name,
									pn_degis_contact.aco_last_name,
									pn_degis_gender.agr_name,
									pn_degis_contact.aco_birthdate,
									pn_degis_contact.aco_telephone,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon
								 FROM 
									samuidemap.pn_degis_contact,
									samuidemap.pn_degis_gender,
                  samuidemap.pn_degis_tumbol
								WHERE  
									pn_degis_contact.aco_tumbol_id = $selectTambon
								AND
									pn_degis_contact.aco_addr_moo = $selectMoo
								AND
								  pn_degis_contact.aco_unit = $selectUnit										
								AND
									pn_degis_contact.aco_addr_house_no = '$selectHouseNo'
                AND
                  pn_degis_tumbol.atb_id = pn_degis_contact.aco_tumbol_id
								AND
									pn_degis_contact.aco_gender_id = pn_degis_gender.agr_id
                AND
                  pn_degis_tumbol.atb_id = $selectTambon 
								ORDER BY 
									pn_degis_contact.aco_first_name";
									
					
					$column = array('id', 'pid', 'house_no', 'first_name', 'last_name', 'gender', 'birthdate', 'telephone', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						$xml = $this->arrayToXML($uid, 
																			$provinceAccessId, 
																			$districtAccessId, 
																			$tambonAccessId, 
																			$objectArray, 
																			$selectTambon, 
																			$selectMoo, 
																			$selectUnit, 
																			$selectHouseNo
																			);
						$xml->save($xmlDirectoryPath.$xmlFileName);	
						unset($xml);
						unset($objectArray);
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}  				
		}
								
		function getPersonByMoo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo){
      $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/male-2.png";
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();

          $sql = "
                   SELECT
										pn_degis_contact.aco_id,	                   
                    pn_degis_contact.aco_person_id,
                    pn_degis_contact.aco_addr_house_no,
                    pn_degis_contact.aco_first_name,
                    pn_degis_contact.aco_last_name,
                    pn_degis_gender.agr_name,
                    pn_degis_contact.aco_birthdate,
                    pn_degis_contact.aco_telephone,
                    pn_degis_contact.aco_lat,
                    pn_degis_contact.aco_lon
                   FROM 
                    samuidemap.pn_degis_contact,
                    samuidemap.pn_degis_gender
                  WHERE  
                    pn_degis_contact.aco_tumbol_id = $selectTambon
                  AND
                    pn_degis_contact.aco_addr_moo = $selectMoo
                  AND
                    pn_degis_contact.aco_gender_id = pn_degis_gender.agr_id
                  ORDER BY 
                    pn_degis_contact.aco_first_name ";

					//echo $sql; exit;
 					$column = array('id', 'pid', 'house_no', 'first_name', 'last_name', 'gender', 'birthdate', 'telephone', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						return $objectArray;
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}  		
		}
		
		
		function getPersonByUnit($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon, $selectMoo, $selectUnit){
      $iconPath = "http://".$_SERVER['SERVER_NAME']."/modules/VoteDataCenter/pnimages/male-2.png";
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();

				$sql = "
								 SELECT
									pn_degis_contact.aco_id,									 
									pn_degis_contact.aco_person_id,
									pn_degis_contact.aco_addr_house_no,									
									pn_degis_contact.aco_first_name,
									pn_degis_contact.aco_last_name,
									pn_degis_gender.agr_name,
									pn_degis_contact.aco_birthdate,
									pn_degis_contact.aco_telephone,
									pn_degis_contact.aco_lat,
									pn_degis_contact.aco_lon
								 FROM 
									samuidemap.pn_degis_contact,
									samuidemap.pn_degis_gender
								WHERE  
									pn_degis_contact.aco_tumbol_id = $selectTambon
								AND
									pn_degis_contact.aco_addr_moo = $selectMoo
								AND
								  pn_degis_contact.aco_unit = $selectUnit										
								AND
									pn_degis_contact.aco_gender_id = pn_degis_gender.agr_id
								ORDER BY 
									pn_degis_contact.aco_first_name";
									
					//echo $sql; exit;
 					$column = array('id', 'pid', 'house_no', 'first_name', 'last_name', 'gender', 'birthdate', 'telephone', 'lat', 'lon');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
					//print_r($objectArray);
					if($objectArray){
						return $objectArray;
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}  		
		}		

  	
  	function arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $selectTambon, $selectMoo, $selectUnit, $selectHouseNo){
  	  //Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;

      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );
      foreach($objectArray as $key => $val){

        $houseNoId = str_replace("/", "-", $selectHouseNo);
        $id = "ps".$selectTambon.$selectMoo.$selectUnit.$houseNoId.$val['id'];
        
        if($val['gender'] == "ชาย"){
        	$gender = "male";
        }else if($val['gender'] == "หญิง"){
        	$gender = "female";        
        }else{
        	$gender = "unknown";                
        }

        $xml_node_data = $xml->createElement("data");
        $xml_node_datas->appendChild( $xml_node_data );
        $xml_node_data->setAttribute("id", $id);
          $xml_node_pid = $xml->createElement("pid",$val['pid']);
          $xml_node_data->appendChild( $xml_node_pid );
          $xml_node_firstname = $xml->createElement("firstname",$val['first_name']);
          $xml_node_data->appendChild( $xml_node_firstname );
          $xml_node_lastname = $xml->createElement("lastname",$val['last_name']);
          $xml_node_data->appendChild( $xml_node_lastname );
          $xml_node_gender = $xml->createElement("gender", $gender);
          $xml_node_data->appendChild( $xml_node_gender );
          $xml_node_birthdate = $xml->createElement("birthdate",$val['birthdate']);
          $xml_node_data->appendChild( $xml_node_birthdate );
          $xml_node_mobilephone = $xml->createElement("mobilephone",$val['telephone']);
          $xml_node_data->appendChild( $xml_node_mobilephone );
          $xml_node_telephone = $xml->createElement("telephone",$val['telephone']);
          $xml_node_data->appendChild( $xml_node_telephone );
          $xml_node_latitude = $xml->createElement("latitude",$val['lat']);
          $xml_node_data->appendChild( $xml_node_latitude );
          $xml_node_longitude = $xml->createElement("longitude",$val['lon']);
          $xml_node_data->appendChild( $xml_node_longitude );
          $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
          $xml_node_data->appendChild( $xml_node_date );
      }

      return $xml;  	
  	}

  	function setResponseNoData(){
     echo '<?xml version="1.0" encoding="UTF-8"?>
            <datas>
              <data id="0">
                <pid></pid>
                <firstname></firstname>
                <lastname></lastname>
                <gender></gender>
                <birthdate></birthdate>
                <mobilephone></mobilephone>
                <telephone></telephone>
                <latitude></latitude> 
                <longitude></longitude> 
                <date>2553-11-11 20:26:57</date>
              </data>
            </datas>';
      pnShutDown();
  	}
  	
  	
  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	} 
  	
  	function writeXMLResponse(){
  	}   	
  }
?>